package com.yl.test;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import com.yl.util.JdkUtil;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wlf
 * @since 2022/8/9
 */
public class WordCountExample {

    public static void main(String[] args) throws Exception {
        JdkUtil.disableWarning();
        // 流环境
        Configuration configuration = new Configuration();
        configuration.setString("execution.savepoint.path", "file:///ttmp/flink/81f8933a10aafd5d5a3026ae2ec173ae/chk-7");
        configuration.setInteger("rest.port", 8081);
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(configuration);

        // 设置保存点
        env.enableCheckpointing(1000);
        // 设置本地保存（如果是windows默认盘为当前项目所在的盘）
        env.getCheckpointConfig().setCheckpointStorage("file:///ttmp/flink/");

        // socket 数据源
        DataStreamSource<String> streamSource = env.socketTextStream("elephant", 6666);
        Map<String, Integer> wordMap = new HashMap<>();
        // 流处理-实时统计单词累计数
        streamSource
                .flatMap((String line, Collector<Tuple2<String, Long>> out) -> {
                            String[] words = line.split(" ");
                            for (String word : words) {
                                out.collect(Tuple2.of(word, 1L));
                            }
                        }
                )
                .returns(Types.TUPLE(Types.STRING, Types.LONG))
                .keyBy(data -> data.f0)
                .sum(1)
                .map(new WordMapFunc(wordMap))
                .print();

        System.out.println("### 流配置完毕 ###");
        // 懒加载
        env.execute();
    }
}
